<?php
/**
 *
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
 * @encoding UTF-8
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class Controller {

    private static $stack = array();

    private static $page;
    private static $args = array();

    /**
     * Fonction qui permet de rendre l'URL plus lisible
     * @example index.php/Mur/CharlyPoly/Post/165433654
     *
     * @todo mettre en place un système de commentaire si l'arg commence par un '-' : /index.php/Mur/CharlyPoly/Post/-J_aime_les_frites/165433654
     * 
     *
     */
    private static function route() {
        $request = str_replace("?", "", $_SERVER['REQUEST_URI']);
        $request = str_replace($_SERVER["QUERY_STRING"], "", $request);
        $arr = explode("/", $request);
        array_shift($arr);
        if (!isset($arr[1]) || empty($arr[1])) {
            self::$page = "Accueil";
        } else {
            include SYS."Pages.const.php";
            if (in_array($arr[1], $Pages)) {
                // On a trouvé la page demandée
                self::$page = $arr[1];
                // On groupe les
                $i = 2;
                while(isset($arr[$i])) {
                    array_push(self::$args, $arr[$i]);
                    ++$i;
                }
            } else {
                self::$page = "Accueil";
            }
        }
    }

    /**
     * Lance la page principale
     */
    public static function init() {
        self::route();
            //Redirige
 
            self::launch(self::$page, self::$args);
    }
    /**
     * @todo Attention au récurisivité infinie !!
     *
     *  Lance une page
     * @param string $PageName
     * @param array $args
     * @return int
     */
    public static function launch($PageName, $args=NULL) {
        include SYS."Pages.const.php";
        if (in_array($PageName, $Pages)) {
            $pagesAlive = array_count_values(self::$stack);
            if (isset($pagesAlive[$PageName]) && $pagesAlive[$PageName] > 10) exit("Erreur, probleme recursivite appel de page !");
            else    array_push (self::$stack, $PageName);
            // Systemes classes : Database, Sessions, Templates
            include SYS.'Page.interface.php';
            // Lancement de la page
            include_once PAGES.$PageName.'.php';
            $page = new $PageName;
            return $page->main($args);
            
//          $page->display();
        } else {
            die ("Controller Error : ".htmlspecialchars($PageName)." n'existe pas !");
        } 
    }
}
?>
